package com.miku.utils;


import io.github.briqt.spark4j.SparkClient;
import io.github.briqt.spark4j.constant.SparkApiVersion;
import io.github.briqt.spark4j.exception.SparkException;
import io.github.briqt.spark4j.model.SparkMessage;
import io.github.briqt.spark4j.model.SparkSyncChatResponse;
import io.github.briqt.spark4j.model.request.SparkRequest;
import io.github.briqt.spark4j.model.response.SparkTextUsage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Component
public class SparkUtils {

    @Autowired
    private SparkClient sparkClient;


    /**
     * 同步调用ai工具类
     *
     * @param systemContent- 系统预设内容
     * @param userContent-   用户输入内容
     * @return
     */
    public  String synchronousCalls(String systemContent, String userContent) {
        List<SparkMessage> messages = new ArrayList<>();
        messages.add(SparkMessage.systemContent(systemContent));
        messages.add(SparkMessage.userContent(userContent));
        // 构造请求
        SparkRequest sparkRequest = SparkRequest.builder()
                // 消息列表
                .messages(messages)
                // 模型回答的tokens的最大长度,非必传，默认为2048。
                // V1.5取值为[1,4096]
                // V2.0取值为[1,8192]
                // V3.0取值为[1,8192]
                .maxTokens(2048)
                // 核采样阈值。用于决定结果随机性,取值越高随机性越强即相同的问题得到的不同答案的可能性越高 非必传,取值为[0,1],默认为0.5
                .temperature(0.2)
                // 指定请求版本，默认使用最新3.0版本
                .apiVersion(SparkApiVersion.V3_0)
                .build();
        try {
            // 同步调用
            SparkSyncChatResponse chatResponse = sparkClient.chatSync(sparkRequest);
            SparkTextUsage textUsage = chatResponse.getTextUsage();

            System.out.println("\n回答：" + chatResponse.getContent());
            System.out.println("\n提问tokens：" + textUsage.getPromptTokens()
                    + "，回答tokens：" + textUsage.getCompletionTokens()
                    + "，总消耗tokens：" + textUsage.getTotalTokens());
            return chatResponse.getContent();
        } catch (SparkException e) {
            System.out.println("发生异常了：" + e.getMessage());
        }
        return "调用失败";
    }

    /**
     * 文本分割工具类
     * @param file
     * @return
     */
    public static ArrayList<String> readerFile(MultipartFile file) {
        ArrayList<String> list= new ArrayList<>();
        try {
            // 获取文件内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
            String line;
            StringBuilder builder = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                // 处理文件内容，例如输出到控制台
                if (builder.length()+line.length()<990){
                    builder.append(line);
                }else {
                    //保存数据库
                    list.add(builder.toString());
                    builder.delete(0,builder.length());
                }
                if (builder.length()==0){
                    builder.append(line);
                }
            }
            //数据可能不超过990
            list.add(builder.toString());
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

}
